package ihm.view;

import ihm.controller.DebtController;
import ihm.controller.MainFrameController;
import ihm.controller.MainFrameController.ContentFrame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	
	private MainFrameController controller;
	private View currentView;
	
	public MainFrame() {
		 try {
	        UIManager.setLookAndFeel(
	            UIManager.getSystemLookAndFeelClassName());
	    } 
	    catch (UnsupportedLookAndFeelException e) {
	    }
	    catch (ClassNotFoundException e) {
	    }
	    catch (InstantiationException e) {
	    }
	    catch (IllegalAccessException e) {
	    }
		
		 this.controller = new MainFrameController();
		initUI();
	}
	
	public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        menubar.add(file);
        setJMenuBar(menubar);

        JToolBar toolbar = new JToolBar();
        
        ImageIcon iconDebts = new ImageIcon("img/debts.png");

        JButton debtsButton = new JButton(iconDebts);
        toolbar.add(debtsButton);
        debtsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	controller.handle(ContentFrame.DEBTS);
            }

        });
        
        ImageIcon iconChart = new ImageIcon("img/chart.png");

        JButton chartButton = new JButton(iconChart);
        toolbar.add(chartButton);
        chartButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	controller.handle(ContentFrame.CHART);
            }

        });
        
        ImageIcon iconPersons = new ImageIcon("img/persons.png");

        JButton personsButton = new JButton(iconPersons);
        toolbar.add(personsButton);
        personsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	controller.handle(ContentFrame.PERSONS);
            }

        });
        
        ImageIcon iconDatabase = new ImageIcon("img/database.png");

        JButton databaseButton = new JButton(iconDatabase);
        toolbar.add(databaseButton);
        databaseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	controller.handle(ContentFrame.DATABASE);
            }

        });

        ImageIcon iconExit = new ImageIcon("img/exit.png");

        JButton exitButton = new JButton(iconExit);
        toolbar.add(exitButton);
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });
        
        add(toolbar, BorderLayout.NORTH);
        
        this.currentView = new DebtController().getView();
        
        setTitle("My debts");
        setSize(1028, 768);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	MainFrame app = new MainFrame();
            	app.setVisible(true);
            }
        });
	}
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected ImageIcon createImageIcon(String path,
	                                           String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
	
	public void setView(View view) {
		//this.remove(comp)
	}
}

